using UnityEngine;
using UnityEngine.Audio;

public class MusicInfo : MonoBehaviour
{
	public AudioMixerSnapshot snap;

	public float time;

	public bool saveLastTimePlay;

	private AudioSource audioS;

	private float lastTimePlay;

	public static string keyLastTime = "keyLastTimeSound_";

	private string keySoundTime
	{
		get
		{
			return keyLastTime + base.gameObject.name;
		}
	}

	public void Awake()
	{
		GetRef();
	}

	public void Play(float forceTime = -1f)
	{
		GetRef();
		if (saveLastTimePlay && audioS != null && audioS.clip != null)
		{
			lastTimePlay = Load.LoadFloat(keySoundTime);
			audioS.time = lastTimePlay;
		}
		if (forceTime > 0f)
		{
			snap.TransitionTo(forceTime);
		}
		else
		{
			snap.TransitionTo(time);
		}
	}

	private void GetRef()
	{
		if (audioS == null)
		{
			audioS = GetComponent<AudioSource>();
		}
	}

	private void Update()
	{
		if (saveLastTimePlay)
		{
			lastTimePlay = audioS.time;
		}
	}

	private void OnDestroy()
	{
		if (saveLastTimePlay && audioS != null && audioS.clip != null)
		{
			Save.SaveFloat(keySoundTime, lastTimePlay);
		}
	}
}
